package com.demo.ctf;

import java.util.concurrent.CompletableFuture;

public class AcceptBothDemo {
    public static void main(String[] args) throws Exception {
        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
            // 模拟耗时操作
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new IllegalStateException(e);
            }
            return 10;
        });

        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
            // 模拟另一个耗时操作
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new IllegalStateException(e);
            }
            return 20;
        });

        CompletableFuture<Void> combinedFuture = future1.thenAcceptBoth(future2, (result1, result2) -> {
            // 当两个future都完成时，执行这里的操作
            System.out.println("Future 1 result: " + result1);
            System.out.println("Future 2 result: " + result2);
            System.out.println("add result: " + result2 + result1);
        });
        // 获取最终的组合结果
        // 主线程等待，防止程序立即退出
        Thread.sleep(2000);
    }
}